var payrollType = 0;// 0 默认 、 1 税前推税后 、 -1 税后推税前
var insuranceBaseType = 0;// 0 默认 、 1 修改过
var fundBaseType = 0;// 0 默认 、 1 修改过
var saveDB = 0;// 0 默认 、 1 保存
var minWage = 0;
var medicarePlan = 0;//大病统筹
var insuranceMin = 0;//社保下限
var medicareMin = 0;//医保下限
var _threshold = 5000;
var arrayNull =
  {
    insuranceMax: "",//社保封顶数
    insuranceMin: 0,//社保下限
    medicareMin: 0,//医保下限
    fundMax: "",//公积金封顶数
    pension: "",//养老金%
    medicare: "",//医疗保险%
    medicarePlan: 0,//大病统筹
    unemploymentInsurance: "",//失业保险%
    fund: "",//公积金%
    threshold: _threshold,//个税起征点
    //单位部分
    pensionFirm: "",//养老金%
    medicareFirm: "",//医疗保险%
    unemploymentInsuranceFirm: "",//失业保险%
    fundFirm: "",//公积金%
    industrialInjuryFirm: "",//工伤保险%
    maternityInsuranceFirm: "",//生育保险%
    minWage: 0//当地最低工资
  };
var arrayInsurance = arrayNull;

function initialValue(arrayObj) {
  $("#insuranceMax").val(arrayObj.insuranceMax);//社保封顶数：
  //$("#fundMax").val(arrayObj.fundMax);//住房公积金封顶数：
  $("#pension").val(arrayObj.pension);//养老保险：
  $("#medicare").val(arrayObj.medicare);//医疗保险：
  $("#unemploymentInsurance").val(arrayObj.unemploymentInsurance);//失业保险：
  $("#fund").val(arrayObj.fund);//公积金：
  //$("#threshold").val(arrayObj.threshold);//个税起征点
  $("#threshold").val(_threshold);//个税起征点
  $("#pensionFirm").val(arrayObj.pensionFirm);//养老金
  $("#medicareFirm").val(arrayObj.medicareFirm);//医疗保险
  $("#unemploymentInsuranceFirm").val(arrayObj.unemploymentInsuranceFirm);//失业保险
  $("#fundFirm").val(arrayObj.fundFirm);//公积金
  $("#industrialInjuryFirm").val(arrayObj.industrialInjuryFirm);//工伤保险
  $("#maternityInsuranceFirm").val(arrayObj.maternityInsuranceFirm);//生育保险

  $("#domestic").attr("checked", true);
  $("#overseas").attr("checked", false);

  minWage = arrayObj.minWage;
  medicarePlan = arrayObj.medicarePlan;
  medicareMin = arrayObj.medicareMin;
  insuranceMin = arrayObj.insuranceMin;
}

function changeType() {
  switch (jQuery.trim($("#taxType").val())) {
    case "0":
      payrollCalculateShow();
      break;
    case "14":
      simplifiedDIVShow();
      break;
  }
}

function simplifiedDIVShow() {
  insuranceBaseType = 0;// 0 默认 、 1 修改过
  fundBaseType = 0;// 0 默认 、 1 修改过

  $("#payrollDIV").hide();//月薪

  //简化版
  $("#simplifiedSalary").val("");
  $("#simplifiedInsurance").val("");
  $("#simplifiedAftertax").val("");
  $("#simplifiedTax").val("");
  $("#simplifiedThreshold").val(_threshold);

  $("#simplifiedDIV").show();
  for (var i = 1; i < 11; i++) {
    $("#DIV" + i.toString()).hide();
  }
  $("#DIV14").hide();
  $("#DIV" + $("#taxType").val()).show();
  $("#otherDIV").show();

}

//工资、薪金所得
function payrollCalculateShow() {
  insuranceBaseType = 0;// 0 默认 、 1 修改过
  fundBaseType = 0;// 0 默认 、 1 修改过

  $("#otherDIV").hide();
  $("#compensationDIV").hide();
  $("#decollator").hide();

  $("#city").val("请选择城市");
  $("#cityId").val("0");
  $("#area").val("0");
  $("#tax").val("");

  $("#personalTotal").html("");
  $("#taxsalary").val("");
  $("#pretaxTotal").html("");
  $("#pension").val("");
  $("#pensionSpan").html("");
  $("#medicare").val("");
  $("#medicareSpan").html("");
  $("#unemploymentInsurance").val("");
  $("#unemploymentInsuranceSpan").html("");
  $("#fund").val("");
  $("#fundSpan").html("");

  $("#firmTotal").html("");
  $("#firmExpenseTotal").html("");
  $("#pensionFirm").val("");
  $("#pensionFirmSpan").html("");
  $("#medicareFirm").val("");
  $("#medicareFirmSpan").html("");
  $("#unemploymentInsuranceFirm").val("");
  $("#unemploymentInsuranceFirmSpan").html("");
  $("#industrialInjuryFirm").val("");
  $("#industrialInjuryFirmSpan").html("");
  $("#maternityInsuranceFirm").val("");
  $("#maternityInsuranceFirmSpan").html("");
  $("#fundFirm").val("");
  $("#fundFirmSpan").html("");

  $("#insuranceBase").val("");
  $("#fundBase").val("");
  $("#insuranceMax").val("");
  $("#threshold").val(_threshold);

  $("#aftertax").val("");//税后
  $("#salary").val("");//

  $("#domestic").attr("checked", true);
  $("#overseas").attr("checked", false);

  $("#payrollDIV").show();
}

function showCitiesDIV(area, cityName, firstChar) {
  var sb = new StringBuilder();
  sb.append("<div style=\"display: block;\" class=\"com_hotresults\" onkeyup=\"changPosition(event);\">");
  sb.append("<div style=\"width: 100%;\">");
  sb.append("<div class=\"ac_title\">");
  sb.append("<span>请选择城市</span>");
  sb.append("<a class=\"ac_close\" href=\"#?\" method=\"close\" title=\"关闭\"></a>");
  sb.append("</div>");

//    #region 分栏行
  sb.append("<ul class=\"AbcSearch clx\" >");
  if (cityName == "" || cityName == "其它" || area == 0) {
    area = 0;
    sb.append("<li class=\"action\">全部</li>");
  } else {
    sb.append("<li onclick=\"changeArea('0');\" onmouseover=\"this.className='ac_over'\" onmouseout=\"this.className=''\">全部</li>");
  }
  if (area == 1 || firstChar == 'a' || firstChar == 'b' || firstChar == 'c' || firstChar == 'd') {
    area = 1;
    sb.append("<li class=\"action\">A-D</li>");
  } else {
    sb.append("<li onclick=\"changeArea('1');\" onmouseover=\"this.className='ac_over'\" onmouseout=\"this.className=''\">A-D</li>");
  }
  if (area == 2 || firstChar == 'e' || firstChar == 'f' || firstChar == 'g' || firstChar == 'h' || firstChar == 'i' || firstChar == 'j') {
    area = 2;
    sb.append("<li class=\"action\" >E-J</li>");
  } else {
    sb.append("<li onclick=\"changeArea('2');\" onmouseover=\"this.className='ac_over'\" onmouseout=\"this.className=''\" >E-J</li>");
  }
  if (area == 3 || firstChar == 'k' || firstChar == 'l' || firstChar == 'm' || firstChar == 'n') {
    area = 3;
    sb.append("<li class=\"action\">K-N</li>");
  } else {
    sb.append("<li onclick=\"changeArea('3');\" onmouseover=\"this.className='ac_over'\" onmouseout=\"this.className=''\">K-N</li>");
  }
  if (area == 4 || firstChar == 'o' || firstChar == 'p' || firstChar == 'q' || firstChar == 'r' || firstChar == 's' || firstChar == 't' || firstChar == 'u' || firstChar == 'v' || firstChar == 'w') {
    area = 4;
    sb.append("<li class=\"action\">O-W</li>");
  } else {
    sb.append("<li onclick=\"changeArea('4');\" onmouseover=\"this.className='ac_over'\" onmouseout=\"this.className=''\">O-W</li>");
  }
  if (area == 5 || firstChar == 'x' || firstChar == 'y' || firstChar == 'z') {
    area = 5;
    sb.append("<li class=\"action\">X-Z</li>");
  } else {
    sb.append("<li onclick=\"changeArea('5');\" onmouseover=\"this.className='ac_over'\" onmouseout=\"this.className=''\">X-Z</li>");
  }
  sb.append("<div style=\"clear:both\"></div>");
  sb.append("</ul>");
//        #endregion  分栏行

  //$("#area").val(area);
  if (cityName == "-1") {
    cityName = "";
  }
  sb.append("<div style=\"clear:both\"></div>");
//        #region 城市部分
  sb.append("<ul style=\"overflow: auto; max-height: 260px;\">");
  var count = 0;
  //alert(groups);
  if (groups.length > area) {
    for (var j = 0; j < groups[area].length; j++) {
//                alert(groups[i][j]);//城市 city  id,name,tax
      if (groups[area][j].name != "其它") {
        var style = "";
        if (groups[area][j].name == "乌鲁木齐" || groups[area][j].name == "呼和浩特") {
          var style = "style='width:52px'";
        }
        sb.append("<li class=\"ac_odd " + (cityName == groups[area][j].name || (cityName == "" && count == 0) ? "ac_over" : "") + "\" id=\"" + area + "_"
          + count + "\" onclick=\"setCity('" + area + "','" + groups[area][j].id + "','" + groups[area][j].name
          + "');\" onmouseover=\"this.className='ac_odd ac_over'\" onmouseout=\"this.className='ac_odd'\" " + style + ">" + groups[area][j].name + "</li>");
        count++;
      }
    }
  }

  sb.append("</ul>");
//        #endregion 城市部分
  sb.append("<input id=\"cityCount\" type=\"hidden\" value=\"" + count + "\" />");
  sb.append("</div>");
  sb.append("<div class=\"clear\"></div>");
  sb.append("</div>");
  return sb.toString();
}

function setCityDIVClass(cityName, area) {
  var exists = false;
  for (var i = 0; i < parseInt($("#cityCount").val()); i++) {
    $("#" + area + "_" + i).removeClass("ac_over");
    if (i == 0) {

      $("#" + area + "_" + i)[0].className = "ac_odd ac_over";
    }
    if ($("#" + area + "_" + i).html() == cityName) {
      exists = true;
      $("#" + area + "_" + i)[0].className = "ac_odd ac_over";
    }
  }
  if (exists) {
    $("#" + area + "_0").removeClass("ac_over");
  }
}

function showCityDIV() {
  HiddenMengBan();
  $("#city").select();
  $("#city").val(jQuery.trim($("#city").val()) == "请选择城市" ? "" : jQuery.trim($("#city").val()));

  var cityName = jQuery.trim($("#city").val()) == "其它" ? "" : jQuery.trim($("#city").val());

  var res = showCitiesDIV(parseInt($("#area").val()), cityName, makePy(cityName));

  if ($("#taxType").val() == "0") {
    $("#cityDIV").html(res);
    $("#cityDIV").show();
  } else {
    $("#compensationCityDIV").html(res);
    $("#compensationCityDIV").show();
  }
  setCityDIVClass(cityName, parseInt($("#area").val()));
}

function hideCityDIV() {
  $("#cityDIV").hide();
}

function setCity(area, cityId, cityName) {
  $("#area").val(area);
  $("#cityId").val(cityId);
  if ($("#taxType").val() == "0") {
    $("#city").val(cityName);
    hideCityDIV();
  }
  changeThisCity();

  for (var i = 0; i < parseInt($("#cityCount").val()); i++) {
    $("#" + $("#area").val() + "_" + i).removeClass("ac_over");
    if ($("#" + $("#area").val() + "_" + i).html() == cityName) {
      $("#" + $("#area").val() + "_" + i)[0].className = "ac_odd ac_over";
    }
  }
}

function changeArea(areaId) {
  if (areaId == "6") {
    $("#area").val("0");
    setCity($("#area").val(), jQuery.trim($("#otherID").val()), "其它");
  } else {
    var cityName = "";
    if ($("#taxType").val() == "0") {
      $("#city").select();
      cityName = jQuery.trim($("#city").val()) == "其它" ? "" : jQuery.trim($("#city").val());
    } else {
      $("#compensationCity").select();
      cityName = jQuery.trim($("#compensationCity").val()) == "其它" ? "" : jQuery.trim($("#city").val());
    }

    var res = showCitiesDIV(parseInt(areaId), "-1", makePy(cityName));

    if ($("#taxType").val() == "0") {
      $("#cityDIV").html(res);
      $("#cityDIV").show();
    } else {
      $("#compensationCityDIV").html(res);
      $("#compensationCityDIV").show();
    }
    setCityDIVClass(cityName, areaId);
  }
}

$(document).ready(function () {
  $(document).click(function () {
    hideCityDIV();
  });
  $('#cityDIV,#cityInptDIV,#compensationCityDIV,#compensationCityInptDIV,#MBContent').click(function (event) {
    event.stopPropagation();
  });
});

function changPosition(event) {
  /*
  var areaId = jQuery.trim($("#area").val());
  if(event.keyCode==13)//回车
  {
      var cityCount = jQuery.trim($("#cityCount").val());
      for(var i =0 ;i < parseInt(cityCount,10) ;i++)
      {
          if( $("#" + areaId + "_" + i.toString()).hasClass("ac_over") )
          {
              App.SelCityName( jQuery.trim($("#" + areaId+"_"+i).html()),changPosition_Callback);
              hideCityDIV();
              return;
          }
      }
  }
  else if(event.keyCode==37)//←
  {
      var cityCount = jQuery.trim($("#cityCount").val());
      var thisIndex = 0;
      for(var i = 0 ;i < parseInt(cityCount,10) ;i++)
      {
          if( $("#" + areaId + "_" + i.toString()).hasClass("ac_over") )
          {
              thisIndex = i;
          }
          $("#" + areaId+"_"+i).removeClass("ac_over");
      }
      if(thisIndex == 0)
      {
          thisIndex = parseInt(cityCount,10) - 1;
      }
      else
      {
          thisIndex--;
      }
      $("#" + areaId+"_"+thisIndex).addClass("ac_over");
  }
  else if(event.keyCode==38)//↑
  {
      var cityCount = jQuery.trim($("#cityCount").val());
      var thisIndex = 0;
      for(var i = 0 ;i < parseInt(cityCount,10) ;i++)
      {
          if( $("#" + areaId + "_" + i.toString()).hasClass("ac_over") )
          {
              thisIndex = i;
          }
          $("#" + areaId+"_"+i).removeClass("ac_over");
      }
      if(thisIndex - 4 < 0)
      {
          thisIndex = parseInt(cityCount,10) - 1;
      }
      else
      {
          thisIndex -= 4;
      }
      $("#" + areaId+"_"+thisIndex).addClass("ac_over");
  }
  else if(event.keyCode==39)//→
  {
      var cityCount = jQuery.trim($("#cityCount").val());
      var thisIndex = 0;
      for(var i = 0 ;i < parseInt(cityCount,10) ;i++)
      {
          if( $("#" + areaId + "_" + i.toString()).hasClass("ac_over") )
          {
              thisIndex = i;
          }
          $("#" + areaId+"_"+i).removeClass("ac_over");
      }
      if(thisIndex == parseInt(cityCount,10) - 1)
      {
          thisIndex = 0;
      }
      else
      {
          thisIndex++;
      }
      $("#" + areaId+"_"+thisIndex).addClass("ac_over");
  }
  else if(event.keyCode==40)//↓
  {
      var cityCount = jQuery.trim($("#cityCount").val());
      var thisIndex = 0;
      for(var i = 0 ;i < parseInt(cityCount,10) ;i++)
      {
          if( $("#" + areaId + "_" + i.toString()).hasClass("ac_over") )
          {
              thisIndex = i;
          }
          $("#" + areaId+"_"+i).removeClass("ac_over");
      }
      if(thisIndex + 4 >= cityCount)
      {
          thisIndex = thisIndex % 4;
      }
      else
      {
          thisIndex += 4;
      }
      $("#" + areaId+"_"+thisIndex).addClass("ac_over");
  }
  */
}

function changPosition_Callback(res) {
  if (res) {
    eval(res.value);
  }
}

function showCityDIVBox() {
  var m_sb = new StringBuilder();
  m_sb.append("<div class=\"meng_k550\">");
  m_sb.append("<div class=\"meng_h\">");
  m_sb.append("<span class=\"f-l orange\">系统提示</span>");
  m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:showCityDIV();\">关闭</a></span>");
  m_sb.append("</div>");
  m_sb.append("<div class=\"meng_bc\" id=\"eCity\">请选择城市。如所在城市未列出，请选择所在地区省会城市，并可根据实际情况修改各项参数。</div>");
  m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" value=\"确定\" onclick=\"showCityDIV();\" class=\"bnt w60\"></div>");
  m_sb.append("</div>");
  $("#MBContent").html(m_sb.toString());
  showMengban();
  $("#btnOK").focus();
}

function changeThisCity() {
  var js_sb = new StringBuilder();
  if ($("#taxType").val() == "0") {
    if (jQuery.trim($("#city").val()) == "" || jQuery.trim($("#city").val()) == "请选择城市") {
      initialValue(arrayNull);

      showCityDIVBox();
    } else {
      changeArrayInsurance(jQuery.trim($("#city").val()));
    }
  } else {
    if (jQuery.trim($("#compensationCity").val()) == "" || jQuery.trim($("#compensationCity").val()) == "请选择城市") {
      showCityDIVBox();
    } else {
      changeArrayInsurance(jQuery.trim($("#compensationCity").val()));
    }
  }
}

function changeArrayInsurance(cName) {
  jQuery("#cityId").value = "";
  if (cName == "其它") {
    var isExist = false;
    var insuranceMaxSUM = 0, fundMaxSUM = 0, insuranceMaxCount = 0;
    for (var i = 1; i < groups.length; i++) {//区域  cities
      for (var j = 0; j < groups[i].length; j++) {//城市 city  id,name,tax
        if (groups[i][j].name != "北京" && groups[i][j].name != "上海" && groups[i][j].name != "深圳" && groups[i][j].name != "广州" && groups[i][j].name != "其它") {
          insuranceMaxSUM += groups[i][j].tax.insuranceMax;
          fundMaxSUM += groups[i][j].tax.fundMax;
          insuranceMaxCount++;
        }
        if (groups[i][j].name == cName) {
          isExist = true;
          jQuery("#cityId").value = groups[i][j].id;
          arrayInsurance = groups[i][j].tax;
        }
      }
    }
  } else {
    for (var i = 0; i < groups.length; i++) {//区域  cities
      if (isExist) {
        break;
      }
      for (var j = 0; j < groups[i].length; j++) {//城市 city  id,name,tax
        if (groups[i][j].name == cName) {
          isExist = true;
          jQuery("#cityId").value = groups[i][j].id;
          arrayInsurance = groups[i][j].tax;
          break;
        }
      }
    }
  }
  if (!isExist) {
    if ($("#taxType").val() == "0") {
      initialValue(arrayNull);
    }
    var m_sb = new StringBuilder();
    m_sb.append("<div class=\"meng_k\">");
    m_sb.append("<div class=\"meng_h\">");
    m_sb.append("<span class=\"f-l orange\">系统提示</span>");
    m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();\">关闭</a></span>");
    m_sb.append("</div>");
    m_sb.append("<div class=\"meng_bc\">请选择正确的城市！</div>");
    m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();\" value=\"确定\" class=\"bnt w60\"></div>");
    m_sb.append("</div>");
    $("#MBContent").html(m_sb.toString());
    showMengban();
    $("#btnOK").focus();
    return;
  }
  if (cName == "其它") {
    arrayInsurance.insuranceMax = (insuranceMaxSUM / insuranceMaxCount).toFixed(2);
    arrayInsurance.fundMax = (fundMaxSUM / insuranceMaxCount).toFixed(2);
  }
  initialValue(arrayInsurance);

  if ($("#taxType").val() == "0") {
    if (payrollType == 1) {
      if (jQuery.trim($("#salary").val()) != "") {
        calculateTaxes();//税前推税后
      }
    } else if (payrollType == -1) {
      if (jQuery.trim($("#aftertax").val()) != "") {
        calculateAftertax();//税后推税前
      }
    }
  } else {
    $("#wagesAVG").val((parseFloat(arrayInsurance.insuranceMax) * 12 / 3).toFixed(0));
    if (jQuery.trim($("#compensationTax").val()) != "") {
      calculateTaxCompensation(0);
    }
  }
}

function calculateTax() {
  payrollType = 1;
  saveDB = 1;
  chkfloat($("#salary")[0]);
  if (jQuery.trim($("#city").val()) != "") {
    if (jQuery.trim($("#salary").val()) != "") {
      calculateTaxes();//税前推税后
    } else {
      var m_sb = new StringBuilder();
      m_sb.append("<div class=\"meng_k\">");
      m_sb.append("<div class=\"meng_h\">");
      m_sb.append("<span class=\"f-l orange\">系统提示</span>");
      m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#salary')[0].focus();\">关闭</a></span>");
      m_sb.append("</div>");
      m_sb.append("<div class=\"meng_bc\">请输入税前月收入！</div>");
      m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();$('#salary')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
      m_sb.append("</div>");
      $("#MBContent").html(m_sb.toString());
      showMengban();
      $("#btnOK").focus();
    }
  } else {
    changeThisCity();
  }
}

function salaryKeydown(event) {
  if (event.keyCode == 13) {
    saveDB = 1;
    calculateTax();
  } else {
    payrollType = 0;
    chkfloatKeyup($("#salary")[0]);

    var salary = jQuery.trim($("#salary").val()) == "" ? "0" : jQuery.trim($("#salary").val());//税前收入
    var insuranceMax = jQuery.trim($("#insuranceMax").val()) != "" ? jQuery.trim($("#insuranceMax").val()) : "0";
    var insuranceBase = 0;
    if (insuranceBaseType == 0) {
      insuranceBase = parseInt(salary, 10) < parseInt(insuranceMax, 10) ? parseInt(salary, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salary, 10));
      if (insuranceBase < insuranceMin) {
        insuranceBase = insuranceMin;
      }

      $("#insuranceBase").val(insuranceBase);
    } else {
      insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
    }

    var fundBase = 0;
    if (fundBaseType == 0) {
      fundBase = parseInt(salary, 10) < parseInt(insuranceMax, 10) ? parseInt(salary, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salary, 10));
      if (fundBase < minWage) {
        fundBase = minWage;
      }
      $("#fundBase").val(fundBase);
    } else {
      fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
    }
//        var fundBase = insuranceBase;
    //个人部分
    var salaryFloat = parseFloat(salary);
    var pension = jQuery.trim($("#pension").val()) != "" ? parseFloat(jQuery.trim($("#pension").val())) : 0;
    var medicare = jQuery.trim($("#medicare").val()) != "" ? parseFloat(jQuery.trim($("#medicare").val())) : 0;
    var unemploymentInsurance = jQuery.trim($("#unemploymentInsurance").val()) != "" ? parseFloat(jQuery.trim($("#unemploymentInsurance").val())) : 0;
    var fund = jQuery.trim($("#fund").val()) != "" ? parseFloat(jQuery.trim($("#fund").val())) : 0;
    var threshold = jQuery.trim($("#threshold").val()) != "" ? parseFloat(jQuery.trim($("#threshold").val())) : 0;
    $("#pensionSpan").html((insuranceBase * pension / 100).toFixed(2));

    var medicareBase = insuranceBase < medicareMin ? medicareMin : insuranceBase;
    $("#medicareSpan").html((medicareBase * medicare / 100 + medicarePlan).toFixed(2));
    $("#unemploymentInsuranceSpan").html((insuranceBase * unemploymentInsurance / 100).toFixed(2));
    $("#fundSpan").html((fundBase * fund / 100).toFixed(2));
    $("#personalTotal").html((parseFloat(jQuery.trim($("#pensionSpan").html())) + parseFloat(jQuery.trim($("#medicareSpan").html())) +
      parseFloat(jQuery.trim($("#unemploymentInsuranceSpan").html())) + parseFloat(jQuery.trim($("#fundSpan").html()))).toFixed(2));

    var taxsalary = parseFloat(salaryFloat - parseFloat($("#personalTotal").html()).toFixed(2)).toFixed(0);
    $("#taxsalary").val(taxsalary > 0 ? taxsalary : 0);

    $("#pretaxTotal").html(((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold) < 0 ? 0 : salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold).toFixed(2));
    var taxSpan = getShuiji((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html()))).toFixed(2), threshold);
    $("#tax").val("");
    $("#aftertax").val("");

    $("#oldaftertax").val("");
    $("#oldtax").val("");
    $("#taxdiffer").val("");
  }
}

function aftertaxKeydown(event) {
  if (event.keyCode == 13) {
    saveDB = 1;
    calculateAftertax1();
  } else {
    payrollType = 0;
    chkfloatKeyup($("#aftertax")[0]);

    var aftertax = jQuery.trim($("#aftertax").val()) == "" ? "0" : jQuery.trim($("#aftertax").val());//税后收入
    var aftertaxFloat = parseFloat(aftertax);
    var insuranceMax = jQuery.trim($("#insuranceMax").val()) != "" ? jQuery.trim($("#insuranceMax").val()) : "0";

    var fundMax = insuranceMax;
    //个人部分
    var pension = jQuery.trim($("#pension").val()) != "" ? parseFloat(jQuery.trim($("#pension").val())) : 0;
    var medicare = jQuery.trim($("#medicare").val()) != "" ? parseFloat(jQuery.trim($("#medicare").val())) : 0;
    var unemploymentInsurance = jQuery.trim($("#unemploymentInsurance").val()) != "" ? parseFloat(jQuery.trim($("#unemploymentInsurance").val())) : 0;
    var fund = jQuery.trim($("#fund").val()) != "" ? parseFloat(jQuery.trim($("#fund").val())) : 0;
    var threshold = jQuery.trim($("#threshold").val()) != "" ? parseFloat(jQuery.trim($("#threshold").val())) : 0;
    var salaryFloat = getSalary(aftertaxFloat, threshold, (pension + medicare + unemploymentInsurance) / 100, fund / 100, insuranceMax, fundMax);
    //$("#salary")[0].value = parseFloat(salaryFloat).toFixed(0);

    var insuranceBase = 0;
    if (insuranceBaseType == 0) {
      insuranceBase = parseInt(salaryFloat, 10) < parseInt(insuranceMax, 10) ? parseInt(salaryFloat, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salaryFloat, 10));
      if (insuranceBase < insuranceMin) {
        insuranceBase = insuranceMin;
      }
      $("#insuranceBase").val(insuranceBase);
    } else {
      insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
    }

    var fundBase = 0;
    if (fundBaseType == 0) {
      fundBase = parseInt(salaryFloat, 10) < parseInt(insuranceMax, 10) ? parseInt(salaryFloat, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salaryFloat, 10));
      if (fundBase < minWage) {
        fundBase = minWage;
      }
      $("#fundBase").val(fundBase);
    } else {
      fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
    }
//        var fundBase = insuranceBase;
    //个人部分
    $("#pensionSpan").html((insuranceBase * pension / 100).toFixed(2));

    var medicareBase = insuranceBase < medicareMin ? medicareMin : insuranceBase;
    $("#medicareSpan").html((medicareBase * medicare / 100 + medicarePlan).toFixed(2));
    $("#unemploymentInsuranceSpan").html((insuranceBase * unemploymentInsurance / 100).toFixed(2));
    $("#fundSpan").html((fundBase * fund / 100).toFixed(2));
    $("#personalTotal").html((parseFloat(jQuery.trim($("#pensionSpan").html())) + parseFloat(jQuery.trim($("#medicareSpan").html())) +
      parseFloat(jQuery.trim($("#unemploymentInsuranceSpan").html())) + parseFloat(jQuery.trim($("#fundSpan").html()))).toFixed(2));

    $("#pretaxTotal").html(((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold) < 0 ? 0 : salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold).toFixed(2));
    var taxSpan = getShuiji((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html()))).toFixed(2), threshold);
    $("#tax").val("");//taxSpan;
    $("#salary").val("");//parseFloat(salaryFloat).toFixed(0);
    $("#taxsalary").val("");

    $("#oldaftertax").val("");
    $("#oldtax").val("");
    $("#taxdiffer").val("");
  }
}

function insuranceBaseKeyup(obj) {
  insuranceBaseType = 1;
  salaryDetailKeyup(obj)
}

function fundBaseKeyup(obj) {
  fundBaseType = 1;
  salaryDetailKeyup(obj)
}

function salaryDetailKeyup(obj) {
  chkfloatKeyup(obj);
  if (payrollType != -1) //税前推税后
  {
    chkfloat($("#salary")[0]);
    var salary = jQuery.trim($("#salary").val()) != "" ? jQuery.trim($("#salary").val()) : "0";//税前收入
    var insuranceMax = jQuery.trim($("#insuranceMax").val()) != "" ? jQuery.trim($("#insuranceMax").val()) : "0";
    var insuranceBase = 0;
    if (insuranceBaseType == 0) {
      insuranceBase = parseInt(salary, 10) < parseInt(insuranceMax, 10) ? parseInt(salary, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salary, 10));
      if (insuranceBase < insuranceMin) {
        insuranceBase = insuranceMin;
      }
      $("#insuranceBase").val(insuranceBase);
    } else {
      insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
    }
    var fundBase = 0;
    if (fundBaseType == 0) {
      fundBase = parseInt(salary, 10) < parseInt(insuranceMax, 10) ? parseInt(salary, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salary, 10));
      if (fundBase < minWage) {
        fundBase = minWage;
      }
      $("#fundBase").val(fundBase);
    } else {
      fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
    }
//        var fundBase = insuranceBase;
    //个人部分
    var salaryFloat = parseFloat(salary);
    var pension = jQuery.trim($("#pension").val()) != "" ? parseFloat(jQuery.trim($("#pension").val())) : 0;
    var medicare = jQuery.trim($("#medicare").val()) != "" ? parseFloat(jQuery.trim($("#medicare").val())) : 0;
    var unemploymentInsurance = jQuery.trim($("#unemploymentInsurance").val()) != "" ? parseFloat(jQuery.trim($("#unemploymentInsurance").val())) : 0;
    var fund = jQuery.trim($("#fund").val()) != "" ? parseFloat(jQuery.trim($("#fund").val())) : 0;
    var threshold = jQuery.trim($("#threshold").val()) != "" ? parseFloat(jQuery.trim($("#threshold").val())) : 0;
    $("#pensionSpan").html((insuranceBase * pension / 100).toFixed(2));
    var medicareBase = insuranceBase < medicareMin ? medicareMin : insuranceBase;
    $("#medicareSpan").html((medicareBase * medicare / 100 + medicarePlan).toFixed(2));
    $("#unemploymentInsuranceSpan").html((insuranceBase * unemploymentInsurance / 100).toFixed(2));
    $("#fundSpan").html((fundBase * fund / 100).toFixed(2));
    $("#personalTotal").html((parseFloat(jQuery.trim($("#pensionSpan").html())) + parseFloat(jQuery.trim($("#medicareSpan").html())) +
      parseFloat(jQuery.trim($("#unemploymentInsuranceSpan").html())) + parseFloat(jQuery.trim($("#fundSpan").html()))).toFixed(2));

    var taxsalary = parseFloat(salaryFloat - parseFloat($("#personalTotal").html()).toFixed(2)).toFixed(0);
    $("#taxsalary").val(taxsalary > 0 ? taxsalary : 0);

    if (salaryFloat < parseFloat(jQuery.trim($("#personalTotal").html()))) {
      var m_sb = new StringBuilder();
      m_sb.append("<div class=\"meng_k\">");
      m_sb.append("<div class=\"meng_h\">");
      m_sb.append("<span class=\"f-l orange\">系统提示</span>");
      m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#salary')[0].focus();\">关闭</a></span>");
      m_sb.append("</div>");
      m_sb.append("<div class=\"meng_bc\">工资真的这么少吗？</div>");
      m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();$('#salary')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
      m_sb.append("</div>");
      $("#MBContent").html(m_sb.toString());
      showMengban();
      $("#btnOK").focus();
      saveDB = 0;
      $("#aftertax").val("");
      $("#tax").val("");
      return;
    } else {
      $("#pretaxTotal").html(((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold) < 0 ? 0 : salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold).toFixed(2));
      var taxSpan = getShuiji((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html()))).toFixed(2), threshold);
      if (payrollType == 1) {
        $("#tax").val(taxSpan);
        $("#aftertax").val((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - taxSpan).toFixed(0));

        //新增
        var taxSpanOld = getShuijiOld((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html()))).toFixed(2), 2000);
        var aftertaxold = (salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - taxSpanOld).toFixed(0);

        $("#oldtax").val(taxSpanOld);
        $("#oldaftertax").val(aftertaxold);
        $("#taxdiffer").val((taxSpanOld - taxSpan).toFixed(2));
      } else {
        $("#tax").val("");
        $("#aftertax").val("");
      }
    }
  } else if (payrollType == -1) //税后推税前
  {
    chkfloat($("#aftertax")[0]);
    var aftertax = jQuery.trim($("#aftertax").val()) == "" ? "0" : jQuery.trim($("#aftertax").val());//税后收入
    var aftertaxFloat = parseFloat(aftertax);
    var insuranceMax = jQuery.trim($("#insuranceMax").val()) != "" ? jQuery.trim($("#insuranceMax").val()) : "0";

    var fundMax = insuranceMax;
    //个人部分
    var pension = jQuery.trim($("#pension").val()) != "" ? parseFloat(jQuery.trim($("#pension").val())) : 0;
    var medicare = jQuery.trim($("#medicare").val()) != "" ? parseFloat(jQuery.trim($("#medicare").val())) : 0;
    var unemploymentInsurance = jQuery.trim($("#unemploymentInsurance").val()) != "" ? parseFloat(jQuery.trim($("#unemploymentInsurance").val())) : 0;
    var fund = jQuery.trim($("#fund").val()) != "" ? parseFloat(jQuery.trim($("#fund").val())) : 0;
    var threshold = jQuery.trim($("#threshold").val()) != "" ? parseFloat(jQuery.trim($("#threshold").val())) : 0;
    var salaryFloat = getSalary(aftertaxFloat, threshold, (pension + medicare + unemploymentInsurance) / 100, fund / 100, parseInt(insuranceMax, 10), fundMax);
    $("#salary").val(parseFloat(salaryFloat).toFixed(0));
    if (parseFloat(salaryFloat) < minWage) {
      var m_sb = new StringBuilder();
      m_sb.append("<div class=\"meng_k\">");
      m_sb.append("<div class=\"meng_h\">");
      m_sb.append("<span class=\"f-l orange\">系统提示</span>");
      m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();\">关闭</a></span>");
      m_sb.append("</div>");
      m_sb.append("<div class=\"meng_bc\">还不到当地最低工资哦！</div>");
      m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();\" value=\"确定\" class=\"bnt w60\"></div>");
      m_sb.append("</div>");
      $("#MBContent").html(m_sb.toString());
      showMengban();
      $("#btnOK").focus();
    }
    var insuranceBase = 0;
    if (insuranceBaseType == 0) {
      insuranceBase = parseInt(salaryFloat, 10) < parseInt(insuranceMax, 10) ? parseInt(salaryFloat, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salaryFloat, 10));
      if (insuranceBase < insuranceMin) {
        insuranceBase < insuranceMin;
      }
      $("#insuranceBase").val(insuranceBase);
    } else {
      insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
    }
    var fundBase = 0;
    if (fundBaseType == 0) {
      fundBase = parseInt(salaryFloat, 10) < parseInt(insuranceMax, 10) ? parseInt(salaryFloat, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salaryFloat, 10));
      if (fundBase < minWage) {
        fundBase = minWage;
      }
      $("#fundBase").val(fundBase);
    } else {
      fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
    }
//        var fundBase = insuranceBase;


    //个人部分
    $("#pensionSpan").html((insuranceBase * pension / 100).toFixed(2));
    var medicareBase = insuranceBase < medicareMin ? medicareMin : insuranceBase;
    $("#medicareSpan").html((medicareBase * medicare / 100 + medicarePlan).toFixed(2));
    $("#unemploymentInsuranceSpan").html((insuranceBase * unemploymentInsurance / 100).toFixed(2));
    $("#fundSpan").html((fundBase * fund / 100).toFixed(2));
    $("#personalTotal").html((parseFloat(jQuery.trim($("#pensionSpan").html())) + parseFloat(jQuery.trim($("#medicareSpan").html())) +
      parseFloat(jQuery.trim($("#unemploymentInsuranceSpan").html())) + parseFloat(jQuery.trim($("#fundSpan").html()))).toFixed(2));


    $("#pretaxTotal").html(((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold) < 0 ? 0 : salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold).toFixed(2));
    var taxSpan = getShuiji((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html()))).toFixed(2), threshold);

    $("#tax").val(taxSpan);
    $("#salary").val(parseFloat(salaryFloat).toFixed(0));

    $("#oldtax").val("");
    $("#oldaftertax").val("");
    $("#taxdiffer").val("");

    var taxsalary = parseFloat(salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())).toFixed(2)).toFixed(0);
    $("#taxsalary").val(taxsalary > 0 ? taxsalary : 0);
  }
}

function fundDetailKeyup(obj) {
  $("#fundFirm").val(jQuery.trim($("#fund").val()));
  salaryDetailKeyup(obj);
}

function fundFirmDetailKeyup(obj) {
  $("#fund").val(jQuery.trim($("#fundFirm").val()));
  salaryDetailKeyup(obj);
}

function calculateTaxes()//税前推税后
{
  if (jQuery.trim($("#city").val()) == "" || jQuery.trim($("#city").val()) == "请选择城市") {
    showCityDIVBox();
    return;
  }
  var salary = jQuery.trim($("#salary").val());//税前收入
  if (salary == "") {
    var m_sb = new StringBuilder();
    m_sb.append("<div class=\"meng_k\">");
    m_sb.append("<div class=\"meng_h\">");
    m_sb.append("<span class=\"f-l orange\">系统提示</span>");
    m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#salary')[0].focus();\">关闭</a></span>");
    m_sb.append("</div>");
    m_sb.append("<div class=\"meng_bc\">请输入税前月收入！</div>");
    m_sb.append("<div class=\"button-margin2\"><input type=\"button\" id=\"btnOK\" onclick=\"HiddenMengBan();$('#salary')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
    m_sb.append("</div>");
    $("#MBContent").html(m_sb.toString());
    showMengban();
    $("#btnOK").focus();
  } else if (parseFloat(salary) < minWage && minWage > 0) {
    var m_sb = new StringBuilder();
    m_sb.append("<div class=\"meng_k\">");
    m_sb.append("<div class=\"meng_h\">");
    m_sb.append("<span class=\"f-l orange\">系统提示</span>");
    m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#salary')[0].focus();\">关闭</a></span>");
    m_sb.append("</div>");
    m_sb.append("<div class=\"meng_bc\">还不到当地最低工资哦！</div>");
    m_sb.append("<div class=\"button-margin2\"><input type=\"button\" id=\"btnOK\" onclick=\"HiddenMengBan();$('#salary')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
    m_sb.append("</div>");
    $("#MBContent").html(m_sb.toString());
    showMengban();
    $("#btnOK").focus();
  } else {
    isCalculate = 0;
    var insuranceBase = 0;
    var insuranceMax = jQuery.trim($("#insuranceMax").val()) != "" ? jQuery.trim($("#insuranceMax").val()) : "0";
    if (insuranceBaseType == 0) {
      insuranceBase = parseInt(salary, 10) < parseInt(insuranceMax, 10) ? parseInt(salary, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salary, 10));
      if (insuranceBase < insuranceMin) {
        insuranceBase = insuranceMin;
      }
      $("#insuranceBase").val(insuranceBase);
    } else {
      insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
    }
    var fundBase = 0;
    if (fundBaseType == 0) {
      fundBase = parseInt(salary, 10) < parseInt(insuranceMax, 10) ? parseInt(salary, 10) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseInt(salary, 10));

      if (fundBase < minWage) {
        fundBase = minWage;
      }

      $("#fundBase").val(fundBase);
    } else {
      fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
    }
//        var fundBase = insuranceBase;
    //个人部分
    var salaryFloat = parseFloat(salary);
    var pension = jQuery.trim($("#pension").val()) != "" ? parseFloat(jQuery.trim($("#pension").val())) : 0;
    var medicare = jQuery.trim($("#medicare").val()) != "" ? parseFloat(jQuery.trim($("#medicare").val())) : 0;
    var unemploymentInsurance = jQuery.trim($("#unemploymentInsurance").val()) != "" ? parseFloat(jQuery.trim($("#unemploymentInsurance").val())) : 0;
    var fund = jQuery.trim($("#fund").val()) != "" ? parseFloat(jQuery.trim($("#fund").val())) : 0;
    var threshold = jQuery.trim($("#threshold").val()) != "" ? parseFloat(jQuery.trim($("#threshold").val())) : 0;
    $("#pensionSpan").html((insuranceBase * pension / 100).toFixed(2));
    var medicareBase = insuranceBase < medicareMin ? medicareMin : insuranceBase;
    $("#medicareSpan").html((medicareBase * medicare / 100 + medicarePlan).toFixed(2));
    $("#unemploymentInsuranceSpan").html((insuranceBase * unemploymentInsurance / 100).toFixed(2));
    $("#fundSpan").html((fundBase * fund / 100).toFixed(2));
    $("#personalTotal").html((parseFloat(jQuery.trim($("#pensionSpan").html())) + parseFloat(jQuery.trim($("#medicareSpan").html())) +
      parseFloat(jQuery.trim($("#unemploymentInsuranceSpan").html())) + parseFloat(jQuery.trim($("#fundSpan").html()))).toFixed(2));
    var taxsalary = parseFloat(salaryFloat - parseFloat($("#personalTotal").html()).toFixed(2)).toFixed(0);
    $("#taxsalary").val(taxsalary > 0 ? taxsalary : 0);
    if (salaryFloat <= parseFloat($("#personalTotal").html())) {
      var m_sb = new StringBuilder();
      m_sb.append("<div class=\"meng_k\">");
      m_sb.append("<div class=\"meng_h\">");
      m_sb.append("<span class=\"f-l orange\">系统提示</span>");
      m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#salary')[0].focus();\">关闭</a></span>");
      m_sb.append("</div>");
      m_sb.append("<div class=\"meng_bc\">工资真的这么少吗？</div>");
      m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();$('#salary')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
      m_sb.append("</div>");
      $("#MBContent").html(m_sb.toString());
      showMengban();
      $("#btnOK").focus();
      saveDB = 0;
      $("#aftertax").val("");
      $("#tax").val("");
      return;
    }

    $("#pretaxTotal").html(((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold) < 0 ? 0 : salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold).toFixed(2));

    var taxSpan = getShuiji((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html()))).toFixed(2), threshold);
    $("#tax").val(taxSpan);
    $("#aftertax").val((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - taxSpan).toFixed(0));
    //新增
    var taxSpanOld = getShuijiOld((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html()))).toFixed(2), 2000);
    var aftertaxold = (salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - taxSpanOld).toFixed(0);

    $("#oldtax").val(taxSpanOld);
    $("#oldaftertax").val(aftertaxold);
    $("#taxdiffer").val((taxSpanOld - taxSpan).toFixed(2));
  }
}

function calculateAftertax1() {
  saveDB = 1;
  payrollType = -1;
  if (jQuery.trim($("#city").val()) == "" || jQuery.trim($("#city").val()) == "请选择城市") {
    initialValue(arrayNull);
    showCityDIVBox();
  } else {
    if (jQuery.trim($("#aftertax").val()) != "") {
      calculateAftertax();//税后推税前
    } else {
      var m_sb = new StringBuilder();
      m_sb.append("<div class=\"meng_k\">");
      m_sb.append("<div class=\"meng_h\">");
      m_sb.append("<span class=\"f-l orange\">系统提示</span>");
      m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#aftertax')[0].focus();\">关闭</a></span>");
      m_sb.append("</div>");
      m_sb.append("<div class=\"meng_bc\">请输入税后月收入！</div>");
      m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();$('#aftertax')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
      m_sb.append("</div>");
      $("#MBContent").html(m_sb.toString());
      showMengban();
      $("#btnOK").focus();
    }
  }
}

function calculateAftertax()//税后推税前
{
  if (jQuery.trim($("#city").val()) == "" || jQuery.trim($("#city").val()) == "请选择城市") {
    showCityDIVBox();
    return;
  }
  var aftertax = jQuery.trim($("#aftertax").val());//税后收入
  if (aftertax == "") {
    var m_sb = new StringBuilder();
    m_sb.append("<div class=\"meng_k\">");
    m_sb.append("<div class=\"meng_h\">");
    m_sb.append("<span class=\"f-l orange\">系统提示</span>");
    m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#aftertax')[0].focus();\">关闭</a></span>");
    m_sb.append("</div>");
    m_sb.append("<div class=\"meng_bc\">请输入税后月收入！</div>");
    m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();$('#aftertax')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
    m_sb.append("</div>");
    $("#MBContent").html(m_sb.toString());
    showMengban();
    $("#btnOK").focus();
  } else {
    var aftertaxFloat = parseFloat(aftertax);
    var insuranceMax = jQuery.trim($("#insuranceMax").val()) != "" ? jQuery.trim($("#insuranceMax").val()) : "0";

    var fundMax = insuranceMax;
    //个人部分
    var pension = jQuery.trim($("#pension").val()) != "" ? parseFloat(jQuery.trim($("#pension").val())) : 0;
    var medicare = jQuery.trim($("#medicare").val()) != "" ? parseFloat(jQuery.trim($("#medicare").val())) : 0;
    var unemploymentInsurance = jQuery.trim($("#unemploymentInsurance").val()) != "" ? parseFloat(jQuery.trim($("#unemploymentInsurance").val())) : 0;
    var fund = jQuery.trim($("#fund").val()) != "" ? parseFloat(jQuery.trim($("#fund").val())) : 0;
    var threshold = jQuery.trim($("#threshold").val()) != "" ? parseFloat(jQuery.trim($("#threshold").val())) : 0;
    var salaryFloat = getSalary(aftertaxFloat, threshold, (pension + medicare + unemploymentInsurance) / 100, fund / 100, parseInt(insuranceMax, 10), fundMax);

    if (parseFloat(salaryFloat) < minWage) {
      var m_sb = new StringBuilder();
      m_sb.append("<div class=\"meng_k\">");
      m_sb.append("<div class=\"meng_h\">");
      m_sb.append("<span class=\"f-l orange\">系统提示</span>");
      m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();\">关闭</a></span>");
      m_sb.append("</div>");
      m_sb.append("<div class=\"meng_bc\">还不到当地最低工资哦！</div>");
      m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();\" value=\"确定\" class=\"bnt w60\"></div>");
      m_sb.append("</div>");
      $("#MBContent").html(m_sb.toString());
      showMengban();
      $("#btnOK").focus();
    }


    var insuranceBase = 0;
    if (insuranceBaseType == 0) {
      insuranceBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
      if (insuranceBase < insuranceMin) {
        insuranceBase = insuranceMin
      }
      $("#insuranceBase").val(insuranceBase);
    } else {
      insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
    }

    var fundBase = 0;
    if (fundBaseType == 0) {
      fundBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
      if (fundBase < minWage) {
        fundBase = minWage;
      }
      $("#fundBase").val(fundBase);
    } else {
      fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
    }
//        var fundBase = insuranceBase;
    //个人部分
    $("#pensionSpan").html((insuranceBase * pension / 100).toFixed(2));
    var medicareBase = insuranceBase < medicareMin ? medicareMin : insuranceBase;
    $("#medicareSpan").html((medicareBase * medicare / 100 + medicarePlan).toFixed(2));
    $("#unemploymentInsuranceSpan").html((insuranceBase * unemploymentInsurance / 100).toFixed(2));
    $("#fundSpan").html((fundBase * fund / 100).toFixed(2));
    $("#personalTotal").html((parseFloat(jQuery.trim($("#pensionSpan").html())) + parseFloat(jQuery.trim($("#medicareSpan").html())) +
      parseFloat(jQuery.trim($("#unemploymentInsuranceSpan").html())) + parseFloat(jQuery.trim($("#fundSpan").html()))).toFixed(2));

    $("#pretaxTotal").html(((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold) < 0 ? 0 : salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html())) - threshold).toFixed(2));
    var taxSpan = getShuiji((salaryFloat - parseFloat(jQuery.trim($("#personalTotal").html()))).toFixed(2), threshold);

    $("#tax").val(taxSpan);
    $("#salary").val(parseFloat(salaryFloat).toFixed(0));
    var taxsalary = parseFloat(salaryFloat - parseFloat($("#personalTotal").html()).toFixed(2)).toFixed(0);
    $("#taxsalary").val(taxsalary > 0 ? taxsalary : 0);

  }
}


function salaryReset() {
  payrollType = 0;// 0 默认 、 1 税前推税后 、 -1 税后推税前
  insuranceBaseType = 0;
  fundBaseType = 0;

  payrollCalculateReset();
  $("#city").val("请选择城市");
  initialValue(arrayNull);
}

function salaryDetailClick() {
  if ($("#domestic").attr("checked")) {
    $("#threshold").val(_threshold);
  } else {
    $("#threshold").val("4800");
  }
  salaryDetailKeyup($("#threshold")[0]);
}

function payrollCalculateReset() {
  $("#tax").val("");

  $("#personalTotal").html("");
  $("#taxsalary").val("");
  $("#pretaxTotal").html("");
  $("#pension").val("");
  $("#pensionSpan").html("");
  $("#medicare").val("");
  $("#medicareSpan").html("");
  $("#unemploymentInsurance").val("");
  $("#unemploymentInsuranceSpan").html("");
  $("#fund").val("");
  $("#fundSpan").html("");

  $("#insuranceBase").val("");
  $("#fundBase").val("");
  $("#insuranceMax").val("");
  $("#threshold").val(_threshold);

  $("#aftertax").val("");//税后
  $("#salary").val("");//

  $("#domestic").attr("checked", true);
  $("#overseas").attr("checked", false);
}

function otherRapidReset() {
  $("#otherBeforeTax").val("");
  $("#otherAfterTax").val("");
  $("#otherTax").val("");
}

//-----------------------------------  工资、薪金、社保 — 年薪制 ---------------------------------------


//----------------------- 简化版 开始
function simplifiedSalaryKeydown(event) {
  if (event.keyCode == 13) {
    simplifiedCalculateSalary();
  } else {
    chkfloatKeyup($("#simplifiedSalary")[0]);

    $("#simplifiedAftertax").val("");
    $("#simplifiedTax").val("");
  }
}

function simplifiedCalculateSalary() {
  chkfloat($("#simplifiedSalary")[0]);
  var simplifiedSalary = jQuery.trim($("#simplifiedSalary").val());
  if (simplifiedSalary == "") {
    var m_sb = new StringBuilder();
    m_sb.append("<div class=\"meng_k\">");
    m_sb.append("<div class=\"meng_h\">");
    m_sb.append("<span class=\"f-l orange\">系统提示</span>");
    m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#simplifiedSalary')[0].focus();\">关闭</a></span>");
    m_sb.append("</div>");
    m_sb.append("<div class=\"meng_bc\">请输入税前月收入！</div>");
    m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();$('#simplifiedSalary')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
    m_sb.append("</div>");
    $("#MBContent").html(m_sb.toString());
    showMengban();
    $("#btnOK")[0].focus();
  } else {
    simplifiedSalary = parseFloat(simplifiedSalary);
    var simplifiedInsurance = jQuery.trim($("#simplifiedInsurance").val()) == "" ? 0 : parseFloat(jQuery.trim($("#simplifiedInsurance").val()));
    var simplifiedThreshold = jQuery.trim($("#simplifiedThreshold").val()) == "" ? 0 : parseFloat(jQuery.trim($("#simplifiedThreshold").val()));
    var taxValue = getShuiji(simplifiedSalary - simplifiedInsurance, simplifiedThreshold);

    $("#simplifiedAftertax").val(simplifiedSalary - simplifiedInsurance - taxValue);
    $("#simplifiedTax").val(taxValue);
    //App.DBAppendOther(simplifiedSalary, 0, 14);
  }
}

function simplifiedAftertaxKeydown(event) {
  if (event.keyCode == 13) {
    simplifiedCalculateAftertax();
  } else {
    chkfloatKeyup($("#simplifiedAftertax")[0]);

    $("#simplifiedSalary").val("");
    $("#simplifiedTax").val("");
  }
}

function simplifiedCalculateAftertax() {
  chkfloat($("#simplifiedAftertax")[0]);
  var simplifiedAftertax = jQuery.trim($("#simplifiedAftertax").val());
  if (simplifiedAftertax == "") {
    var m_sb = new StringBuilder();
    m_sb.append("<div class=\"meng_k\">");
    m_sb.append("<div class=\"meng_h\">");
    m_sb.append("<span class=\"f-l orange\">系统提示</span>");
    m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#simplifiedAftertax')[0].focus();\">关闭</a></span>");
    m_sb.append("</div>");
    m_sb.append("<div class=\"meng_bc\">请输入税后月收入！</div>");
    m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();$('#simplifiedAftertax')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
    m_sb.append("</div>");
    $("#MBContent").html(m_sb.toString());
    showMengban();
    $("#btnOK").focus();
  } else {
    simplifiedAftertax = parseFloat(simplifiedAftertax);
    var simplifiedInsurance = jQuery.trim($("#simplifiedInsurance").val()) == "" ? 0 : parseFloat(jQuery.trim($("#simplifiedInsurance").val()));
    var simplifiedThreshold = jQuery.trim($("#simplifiedThreshold").val()) == "" ? 0 : parseFloat(jQuery.trim($("#simplifiedThreshold").val()));

    var simplifiedSalary = getSimplifiedSalary(simplifiedAftertax, simplifiedThreshold, simplifiedInsurance);

    var taxValue = getShuiji(simplifiedSalary - simplifiedInsurance, simplifiedThreshold);

    $("#simplifiedSalary").val(simplifiedSalary);
    $("#simplifiedTax").val(taxValue);
    //App.DBAppendOther(simplifiedAftertax, 1, 14);
  }
}

function simplifiedTaxKeydown(event) {
  if (event.keyCode == 13) {
    simplifiedCalculateTax();
  } else {
    chkfloatKeyup($("#simplifiedTax")[0]);

    $("#simplifiedSalary").val("");
    $("#simplifiedAftertax").val("");
  }
}

function simplifiedCalculateTax() {
  chkfloat($("#simplifiedTax")[0]);
  var simplifiedTax = jQuery.trim($("#simplifiedTax").val());
  if (simplifiedTax == "") {
    var m_sb = new StringBuilder();
    m_sb.append("<div class=\"meng_k\">");
    m_sb.append("<div class=\"meng_h\">");
    m_sb.append("<span class=\"f-l orange\">系统提示</span>");
    m_sb.append("<span class=\"f-r orange\"><a href=\"javascript:HiddenMengBan();$('#simplifiedTax')[0].focus();\">关闭</a></span>");
    m_sb.append("</div>");
    m_sb.append("<div class=\"meng_bc\">请输入缴纳个税！</div>");
    m_sb.append("<div class=\"button-margin2\"><input id=\"btnOK\" type=\"button\" onclick=\"HiddenMengBan();$('#simplifiedTax')[0].focus();\" value=\"确定\" class=\"bnt w60\"></div>");
    m_sb.append("</div>");
    $("#MBContent").html(m_sb.toString());
    showMengban();
    $("#btnOK").focus();
  } else {
    simplifiedTax = parseFloat(simplifiedTax);
    var simplifiedInsurance = jQuery.trim($("#simplifiedInsurance").val()) == "" ? 0 : parseFloat(jQuery.trim($("#simplifiedInsurance").val()));
    var simplifiedThreshold = jQuery.trim($("#simplifiedThreshold").val()) == "" ? 0 : parseFloat(jQuery.trim($("#simplifiedThreshold").val()));

    var simplifiedSalary = getSimplifiedSalaryByTax(simplifiedTax, simplifiedThreshold, simplifiedInsurance);


    $("#simplifiedSalary").val(simplifiedSalary);
    $("#simplifiedAftertax").val((simplifiedSalary - simplifiedInsurance - simplifiedTax).toFixed(2));
    //App.DBAppendOther(simplifiedTax, 2, 14);
  }
}

//----------------------- 简化版 结束


function getShuiji(salario, threshold) {
  var zx = document.getElementById("zx").value;
  if (zx == '') {
    zx = 0;
  }
  if (salario < threshold) {
    return 0;
  } else {
    var salarioV = salario - threshold - zx;
    if (salarioV <= 0) return 0;
    if (salarioV <= 3000) {
      return (salarioV * 0.03).toFixed(2);
    } else if (salarioV > 3000 && salarioV <= 12000) {
      return (salarioV * 0.1 - 210).toFixed(2);
    } else if (salarioV > 12000 && salarioV <= 25000) {
      return (salarioV * 0.2 - 1410).toFixed(2);
    } else if (salarioV > 25000 && salarioV <= 35000) {
      return (salarioV * 0.25 - 2660).toFixed(2);
    } else if (salarioV > 35000 && salarioV <= 55000) {
      return (salarioV * 0.3 - 4410).toFixed(2);
    } else if (salarioV > 55000 && salarioV <= 80000) {
      return (salarioV * 0.35 - 7160).toFixed(2);
    } else if (salarioV > 80000) {
      return (salarioV * 0.45 - 15160).toFixed(2);
    }
  }
}

function getShuijiOld(salario, threshold) {
  if (salario < threshold) {
    return 0;
  } else {
    var salarioV = salario - threshold;
    if (salarioV <= 500) {
      return (salarioV * 0.05).toFixed(2);
    } else if (salarioV > 500 && salarioV <= 2000) {
      return (salarioV * 0.1 - 25).toFixed(2);
    } else if (salarioV > 2000 && salarioV <= 5000) {
      return (salarioV * 0.15 - 125).toFixed(2);
    } else if (salarioV > 5000 && salarioV <= 20000) {
      return (salarioV * 0.20 - 375).toFixed(2);
    } else if (salarioV > 20000 && salarioV <= 40000) {
      return (salarioV * 0.25 - 1375).toFixed(2);
    } else if (salarioV > 40000 && salarioV <= 60000) {
      return (salarioV * 0.30 - 3375).toFixed(2);
    } else if (salarioV > 60000 && salarioV <= 80000) {
      return (salarioV * 0.35 - 6375).toFixed(2);
    } else if (salarioV > 80000 && salarioV <= 100000) {
      return (salarioV * 0.40 - 10375).toFixed(2);
    } else if (salarioV > 100000) {
      return (salarioV * 0.45 - 15375).toFixed(2);
    }
  }
}

function getSalary(aftertaxFloat, threshold, percentageInsurance, percentageFund, insuranceMax, fundMax) {
  var fundBase = 0.0, insuranceBase = 0.0, thisInsurance = 0.0, thisFund = 0.0, salaryFloat = 0.0;
  if (aftertaxFloat < threshold) {
    insuranceBase = (aftertaxFloat + medicarePlan) / (1 - (percentageInsurance + percentageFund));
    fundBase = insuranceBase;
    if (insuranceBaseType == 0) {
      if (insuranceBase < insuranceMin) {
        insuranceBase = insuranceMin;
      }
    } else {
      insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
    }
    if (fundBaseType == 0) {
      if (fundBase < minWage) {
        fundBase = minWage;
      }
    } else {
      fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
    }
    thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
    thisFund = fundBase * percentageFund;
    salaryFloat = (aftertaxFloat + thisInsurance + thisFund)
    if (salaryFloat > insuranceBase) {
      insuranceBase = salaryFloat;
    }
    if (salaryFloat > fundBase) {
      fundBase = salaryFloat;
    }
    thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
    thisFund = fundBase * percentageFund;
    return (aftertaxFloat + thisInsurance + thisFund);
    //return (aftertaxFloat + medicarePlan) / (1 - (percentageInsurance + percentageFund));
  } else {
    var percentage = percentageInsurance + percentageFund;
    /////// --------- 3% ---------

    salaryFloat = (aftertaxFloat - (threshold * 0.03) + (1 - 0.03) * medicarePlan) / (1 - percentage - 0.03 + percentage * 0.03);
    if (insuranceBaseType == 0) {
      insuranceBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
      if (insuranceBase < insuranceMin) {
        insuranceBase = insuranceMin;
      }
    } else {
      insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
    }

    if (fundBaseType == 0) {
      fundBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
      if (fundBase < minWage) {
        fundBase = minWage;
      }
    } else {
      fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
    }
//        fundBase = insuranceBase;

    thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
    thisFund = fundBase * percentageFund;

    if (parseInt(insuranceBase, 10) != parseInt(salaryFloat, 10) || parseInt(fundBase, 10) != parseInt(salaryFloat, 10)) {
      salaryFloat = (aftertaxFloat - (threshold * 0.03) + (thisInsurance + thisFund) * (1 - 0.03)) / (1 - 0.03)
    }

    var thisAftertaxFloat = salaryFloat - (thisInsurance + thisFund) - (salaryFloat - (thisInsurance + thisFund) - threshold) * 0.03;
    if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - (thisInsurance + thisFund) - threshold) <= 1500) {
      return (salaryFloat).toFixed(2);
    } else {/////////-------------------------- 10% -----------------
      salaryFloat = (aftertaxFloat - 105 - (threshold * 0.10) + (1 - 0.10) * medicarePlan) / (1 - percentage - 0.10 + percentage * 0.10);

      if (insuranceBaseType == 0) {
        insuranceBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
        if (insuranceBase < insuranceMin) {
          insuranceBase = insuranceMin;
        }
      } else {
        insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
      }

      if (fundBaseType == 0) {
        fundBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
        if (fundBase < minWage) {
          fundBase = minWage;
        }
      } else {
        fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
      }
//            fundBase = insuranceBase;

      thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
      thisFund = fundBase * percentageFund;

      if (parseInt(insuranceBase, 10) != parseInt(salaryFloat, 10) || parseInt(fundBase, 10) != parseInt(salaryFloat, 10)) {
        salaryFloat = (aftertaxFloat - 105 - (threshold * 0.10) + (thisInsurance + thisFund) * (1 - 0.10)) / (1 - 0.10)
      }

      thisAftertaxFloat = salaryFloat - (thisInsurance + thisFund) - ((salaryFloat - (thisInsurance + thisFund) - threshold) * 0.10 - 105);
      if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - (thisInsurance + thisFund) - threshold) > 1500 && (salaryFloat - (thisInsurance + thisFund) - threshold) <= 4500) {
        return (salaryFloat).toFixed(2);
      } else {////----------------------------- 20% ------------------------
        salaryFloat = (aftertaxFloat - 555 - (threshold * 0.20) + (1 - 0.20) * medicarePlan) / (1 - percentage - 0.20 + percentage * 0.20);

        if (insuranceBaseType == 0) {
          insuranceBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
          if (insuranceBase < insuranceMin) {
            insuranceBase = insuranceMin;
          }
        } else {
          insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
        }
        if (fundBaseType == 0) {
          fundBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
          if (fundBase < minWage) {
            fundBase = minWage;
          }
        } else {
          fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
        }
//                fundBase = insuranceBase;

        thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
        thisFund = fundBase * percentageFund;

        if (parseInt(insuranceBase, 10) != parseInt(salaryFloat, 10) || parseInt(fundBase, 10) != parseInt(salaryFloat, 10)) {
          salaryFloat = (aftertaxFloat - 555 - (threshold * 0.20) + (thisInsurance + thisFund) * (1 - 0.20)) / (1 - 0.20)
        }

        thisAftertaxFloat = salaryFloat - (thisInsurance + thisFund) - ((salaryFloat - (thisInsurance + thisFund) - threshold) * 0.20 - 555);
        if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - (thisInsurance + thisFund) - threshold) > 4500 && (salaryFloat - (thisInsurance + thisFund) - threshold) <= 9000) {
          return (salaryFloat).toFixed(2);
        } else {///////////---------------------- 25% ----------------------
          salaryFloat = (aftertaxFloat - 1005 - (threshold * 0.25) + (1 - 0.25) * medicarePlan) / (1 - percentage - 0.25 + percentage * 0.25);

          if (insuranceBaseType == 0) {
            insuranceBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
            if (insuranceBase < insuranceMin) {
              insuranceBase = insuranceMin;
            }
          } else {
            insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
          }
          if (fundBaseType == 0) {
            fundBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
            if (fundBase < minWage) {
              fundBase = minWage;
            }
          } else {
            fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
          }
//                    fundBase = insuranceBase;

          thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
          thisFund = fundBase * percentageFund;

          if (parseInt(insuranceBase, 10) != parseInt(salaryFloat, 10) || parseInt(fundBase, 10) != parseInt(salaryFloat, 10)) {
            salaryFloat = (aftertaxFloat - 1005 - (threshold * 0.25) + (thisInsurance + thisFund) * (1 - 0.25)) / (1 - 0.25)
          }

          thisAftertaxFloat = salaryFloat - (thisInsurance + thisFund) - ((salaryFloat - (thisInsurance + thisFund) - threshold) * 0.25 - 1005);

          if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - (thisInsurance + thisFund) - threshold) > 9000 && (salaryFloat - (thisInsurance + thisFund) - threshold) <= 35000) {
            return (salaryFloat).toFixed(2);
          } else {///////--------------------------- 30% ----------------------
            salaryFloat = (aftertaxFloat - 2755 - (threshold * 0.30) + (1 - 0.30) * medicarePlan) / (1 - percentage - 0.30 + percentage * 0.30);
            if (insuranceBaseType == 0) {
              insuranceBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
              if (insuranceBase < insuranceMin) {
                insuranceBase = insuranceMin;
              }
            } else {
              insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
            }
            if (fundBaseType == 0) {
              fundBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
              if (fundBase < minWage) {
                fundBase = minWage;
              }
            } else {
              fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
            }
//                        fundBase = insuranceBase;

            thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
            thisFund = fundBase * percentageFund;

            if (parseInt(insuranceBase, 10) != parseInt(salaryFloat, 10) || parseInt(fundBase, 10) != parseInt(salaryFloat, 10)) {
              salaryFloat = (aftertaxFloat - 2755 - (threshold * 0.30) + (thisInsurance + thisFund) * (1 - 0.30)) / (1 - 0.30)
            }

            thisAftertaxFloat = salaryFloat - (thisInsurance + thisFund) - ((salaryFloat - (thisInsurance + thisFund) - threshold) * 0.30 - 2755);

            if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - (thisInsurance + thisFund) - threshold) > 35000 && (salaryFloat - (thisInsurance + thisFund) - threshold) <= 55000) {
              return (salaryFloat).toFixed(2);
            } else {/////////---------------------- 35% ------------------
              salaryFloat = (aftertaxFloat - 5505 - (threshold * 0.35) + (1 - 0.35) * medicarePlan) / (1 - percentage - 0.35 + percentage * 0.35);

              if (insuranceBaseType == 0) {
                insuranceBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
                if (insuranceBase < insuranceMin) {
                  insuranceBase = insuranceMin;
                }
              } else {
                insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
              }
              if (fundBaseType == 0) {
                fundBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
                if (fundBase < minWage) {
                  fundBase = minWage;
                }
              } else {
                fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
              }
//                            fundBase = insuranceBase;

              thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
              thisFund = fundBase * percentageFund;

              if (parseInt(insuranceBase, 10) != parseInt(salaryFloat, 10) || parseInt(fundBase, 10) != parseInt(salaryFloat, 10)) {
                salaryFloat = (aftertaxFloat - 5505 - (threshold * 0.35) + (thisInsurance + thisFund) * (1 - 0.35)) / (1 - 0.35)
              }

              thisAftertaxFloat = salaryFloat - (thisInsurance + thisFund) - ((salaryFloat - (thisInsurance + thisFund) - threshold) * 0.35 - 5505);

              if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - (thisInsurance + thisFund) - threshold) > 55000 && (salaryFloat - (thisInsurance + thisFund) - threshold) <= 80000) {
                return (salaryFloat).toFixed(2);
              } else {///////////----------------- 45% ----------------
                salaryFloat = (aftertaxFloat - 13505 - (threshold * 0.45) + (1 - 0.45) * medicarePlan) / (1 - percentage - 0.45 + percentage * 0.45);

                if (insuranceBaseType == 0) {
                  insuranceBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
                  if (insuranceBase < insuranceMin) {
                    insuranceBase = insuranceMin;
                  }
                } else {
                  insuranceBase = jQuery.trim($("#insuranceBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#insuranceBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#insuranceBase").val())));
                }
                if (fundBaseType == 0) {
                  fundBase = parseFloat(salaryFloat).toFixed(0) < parseInt(insuranceMax, 10) ? parseFloat(salaryFloat).toFixed(0) : (parseInt(insuranceMax, 10) > 0 ? parseInt(insuranceMax, 10) : parseFloat(salaryFloat).toFixed(0));
                  if (fundBase < minWage) {
                    fundBase = minWage;
                  }
                } else {
                  fundBase = jQuery.trim($("#fundBase").val()) == "" ? 0 : (parseFloat(jQuery.trim($("#fundBase").val())) > parseFloat(insuranceMax) ? parseFloat(insuranceMax) : parseFloat(jQuery.trim($("#fundBase").val())));
                }
//                                        fundBase = insuranceBase;

                thisInsurance = insuranceBase * percentageInsurance + medicarePlan;
                thisFund = fundBase * percentageFund;

                if (parseInt(insuranceBase, 10) != parseInt(salaryFloat, 10) || parseInt(fundBase, 10) != parseInt(salaryFloat, 10)) {
                  salaryFloat = (aftertaxFloat - 13505 - (threshold * 0.45) + (thisInsurance + thisFund) * (1 - 0.45)) / (1 - 0.45)
                }

                thisAftertaxFloat = salaryFloat - (thisInsurance + thisFund) - ((salaryFloat - (thisInsurance + thisFund) - threshold) * 0.45 - 13505);
                return (salaryFloat).toFixed(2);
              }
            }
          }
        }
      }
    }
  }
}

function getShuijiNian(salario) {
  var salarioV = salario / 12;
  if (salarioV <= 1500) {
    return (salario * 0.03).toFixed(2);
  } else if (salarioV > 1500 && salarioV <= 4500) {
    return (salario * 0.1 - 105).toFixed(2);
  } else if (salarioV > 4500 && salarioV <= 9000) {
    return (salario * 0.20 - 555).toFixed(2);
  } else if (salarioV > 9000 && salarioV <= 35000) {
    return (salario * 0.25 - 1005).toFixed(2);
  } else if (salarioV > 35000 && salarioV <= 55000) {
    return (salario * 0.30 - 2755).toFixed(2);
  } else if (salarioV > 55000 && salarioV <= 80000) {
    return (salario * 0.35 - 5505).toFixed(2);
  } else if (salarioV > 80000) {
    return (salario * 0.45 - 13505).toFixed(2);
  }
}

function getSimplifiedSalary(aftertaxFloat, threshold, insurance) {
  if (aftertaxFloat < threshold) {
    return aftertaxFloat;
  } else {
    /////// --------- 3% ---------

    var salaryFloat = (aftertaxFloat - (threshold * 0.03) + insurance * (1 - 0.03)) / (1 - 0.03);

    var thisAftertaxFloat = salaryFloat - insurance - (salaryFloat - insurance - threshold) * 0.03;

    if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - insurance - threshold) <= 1500) {
      return (salaryFloat).toFixed(2);
    } else {/////////-------------------------- 10% -----------------
      salaryFloat = (aftertaxFloat - 105 - (threshold * 0.10) + insurance * (1 - 0.10)) / (1 - 0.10);

      thisAftertaxFloat = salaryFloat - insurance - (salaryFloat - insurance - threshold) * 0.10 + 105;

      if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - insurance - threshold) > 1500 && (salaryFloat - insurance - threshold) <= 4500) {
        return (salaryFloat).toFixed(2);
      }
      {///////////---------------------- 20% ----------------------
        salaryFloat = (aftertaxFloat - 555 - (threshold * 0.20) + insurance * (1 - 0.20)) / (1 - 0.20);

        thisAftertaxFloat = salaryFloat - insurance - ((salaryFloat - insurance - threshold) * 0.20 - 555);

        if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - insurance - threshold) > 4500 && (salaryFloat - insurance - threshold) <= 9000) {
          return (salaryFloat).toFixed(2);
        } else {///////--------------------------- 25% ----------------------
          salaryFloat = (aftertaxFloat - 1005 - (threshold * 0.25) + insurance * (1 - 0.25)) / (1 - 0.25);

          thisAftertaxFloat = salaryFloat - insurance - ((salaryFloat - insurance - threshold) * 0.25 - 1005);

          if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - insurance - threshold) > 9000 && (salaryFloat - insurance - threshold) <= 35000) {
            return (salaryFloat).toFixed(2);
          } else {/////////---------------------- 30% ------------------
            salaryFloat = (aftertaxFloat - 2725 - (threshold * 0.30) + insurance * (1 - 0.30)) / (1 - 0.30);

            thisAftertaxFloat = salaryFloat - insurance - ((salaryFloat - insurance - threshold) * 0.30 - 2725);

            if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - insurance - threshold) > 35000 && (salaryFloat - insurance - threshold) <= 55000) {
              return (salaryFloat).toFixed(2);
            } else {/////////////-------------------- 35% ------------------
              salaryFloat = (aftertaxFloat - 5505 - (threshold * 0.35) + insurance * (1 - 0.35)) / (1 - 0.35);

              thisAftertaxFloat = salaryFloat - insurance - ((salaryFloat - insurance - threshold) * 0.35 - 5505);

              if (parseFloat(aftertaxFloat).toFixed(0) == parseFloat(thisAftertaxFloat).toFixed(0) && (salaryFloat - insurance - threshold) > 55000 && (salaryFloat - insurance - threshold) <= 80000) {
                return (salaryFloat).toFixed(2);
              } else {///////////----------------- 45% ----------------
                salaryFloat = (aftertaxFloat - 13505 - (threshold * 0.45) + insurance * (1 - 0.45)) / (1 - 0.45);

                return (salaryFloat).toFixed(2);
              }
            }
          }
        }
      }
    }
  }
}

function getSimplifiedSalaryByTax(tax, threshold, insurance) {
  if (tax <= 45) {
    return (tax / 0.03 + threshold + insurance).toFixed(2);
  } else if (tax > 45 && tax <= 450) {
    return ((tax + 105) / 0.1 + threshold + insurance).toFixed(2);
  } else if (tax > 450 && tax <= 1800) {
    return ((tax + 555) / 0.20 + threshold + insurance).toFixed(2);
  } else if (tax > 1800 && tax <= 8750) {
    return ((tax + 1005) / 0.25 + threshold + insurance).toFixed(2);
  } else if (tax > 8750 && tax <= 16500) {
    return ((tax + 2725) / 0.30 + threshold + insurance).toFixed(2);
  } else if (tax > 16500 && tax <= 28000) {
    return ((tax + 5505) / 0.35 + threshold + insurance).toFixed(2);
  } else if (tax > 28000) {
    return ((tax + 13505) / 0.45 + threshold + insurance).toFixed(2);
  }
}